package com.songyuquan.webcs.controller;

import com.songyuquan.webcs.pojo.Orders;
import com.songyuquan.webcs.pojo.User;
import com.songyuquan.webcs.service.OrdersService;
import com.songyuquan.webcs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户列表控制器
 */
@Controller
@RequestMapping("/back")
public class BackUserController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrdersService ordersService;

    //显示用户列表信息
    @GetMapping("/userList")
    public ModelAndView getUserList() {
        ModelAndView modelAndView = new ModelAndView();
        List<User> list = userService.list();
        modelAndView.addObject("userList", list);
        modelAndView.setViewName("back/userList");
        return modelAndView;
    }

    //显示用户权限列表
    @GetMapping("/userType")
    public ModelAndView getUserType() {
        ModelAndView modelAndView = new ModelAndView();
        List<User> list = userService.list();
        modelAndView.addObject("userList", list);
        modelAndView.setViewName("back/userType");
        return modelAndView;
    }

    //给用户授予权限
    @GetMapping("/updateType/{id}")
    public String setUpdateType(@PathVariable("id") Integer id) {
        User user = userService.getById(id);
        user.setType(true);
        userService.updateById(user);
        return "redirect:/back/userType";
    }

    //删除用户权限
    @GetMapping("/deleteType/{id}")
    public String setDeleteType(@PathVariable("id") Integer id) {
        User user = userService.getById(id);
        user.setType(false);
        userService.updateById(user);
        return "redirect:/back/userType";
    }

    //显示订单列表详情
    @GetMapping("/ordersList")
    public ModelAndView getOrdersList() {
        ModelAndView modelAndView = new ModelAndView();
        List<Orders> list = ordersService.list();
        modelAndView.addObject("orderList", list);
        modelAndView.setViewName("back/ordersList");
        return modelAndView;
    }

    //订单发货
    @GetMapping("/goods/{id}")
    public String setGoods(@PathVariable("id") Integer id) {
        Orders orders = ordersService.getById(id);
        orders.setGoods("已发货");
        ordersService.updateById(orders);
        return "redirect:/back/ordersList";
    }

    //订单信息导出
    @GetMapping("/daochu")
    public String daochu(HttpServletResponse response) {
        ordersService.daochu(response);
        return "redirect:/back/ordersList";
    }
}
